﻿using FPSO.HMI.Controls;
using FPSO.HMI.Utils;
using FPSO.Models.Constant;
using FPSO.Models.Domain;
using FPSO.Models.ViewModel.Controls;
using FPSO.Services;
using FPSO.Services.Impl;
using MySql.Data.MySqlClient;
using Opc.Ua;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Xml.Linq;

namespace FPSO.HMI.Views.Dialog
{
    public partial class UserManageDialog : Window
    {
		private UCUserInfoListViewModel viewModel;
		private DBUserInfoService g_Service = new DBUserInfoService();
		private int selected_index = -1;
		public UserManageDialog()
        {
			InitializeComponent();
			viewModel = new UCUserInfoListViewModel();
			this.DataContext = viewModel;
			LoadData();
		}

		private bool LoadData()
		{
			try
			{
				List<UserInfo> users = g_Service.GetAllUserInfos();
				if(users.Count() > 0)
				{
					viewModel.UserList = users;
				}
				return true;
				//}
			}
			catch (Exception ex)
			{
				CommUtil.AddSystemLog(7, 11, ex.Message);
				return false;
			}
		}
		
		/// <summary>
		/// 退出事件
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void ExitButton_Click(object sender, RoutedEventArgs e)
        {
            //返回false以阻止后续流程并退出 
            this.DialogResult = false;
        }

        /// <summary>
        /// 窗口拖动事件
        /// </summary>
        private void Grid_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            if (e.ChangedButton == MouseButton.Left)
            {
                DragMove();
            }
        }

		private void CreateButton_Click(object sender, RoutedEventArgs e)
		{
			UserEdidDialog userEdidDialog = new UserEdidDialog(0);
			userEdidDialog.ShowDialog();
		}

		private void UpdateButton_Click(object sender, RoutedEventArgs e)
		{
			UserInfo selected_item = (UserInfo)UserInfoGrid.SelectedItem;
			if (selected_item != null)
			{
				UserEdidDialog userEdidDialog = new UserEdidDialog(1);
				userEdidDialog.ShowDialog();
			}
		}

		private void DeleteButton_Click(object sender, RoutedEventArgs e)
		{
			UserInfo selected_item = (UserInfo)UserInfoGrid.SelectedItem;
			if (selected_item != null)
			{
				string current_user = AuthUtil.GetLoginUser();
				if(current_user != selected_item.UserName)
					g_Service.DeleteUserInfo(selected_item.UserId);
			}
		}

		private void DataGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
		{
		}
	}
}
